package net.trade.trade.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.core.base.dao.CommonDAO;
import net.core.common.DateFormater;
import net.core.common.StrUtils;
import net.core.common.SysConfigUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TradeReportServiceImpl implements ITradeReportService {
	@Autowired
	private CommonDAO commonDAO;
	@Override
	public List getDayProfitRank() {
		String day = DateFormater.getToday();
		String startTimeCfg = SysConfigUtil.getConfigValue("TRADE_START", "TRADECFG");
		String endTimeCfg = SysConfigUtil.getConfigValue("TRADE_END", "TRADECFG");
		String startTime = day+" "+startTimeCfg+":00";
		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.DATE, 1);
		SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd");
		String endTime = formatter1.format(cal.getTime())+" "+endTimeCfg+":00";;
		
		
		String sql = "SELECT b.su_name,a.pal from(" +
				"SELECT su_id,sum(bl_pl) pal FROM t_tradelog t " +
				"where 1=1 and t.bl_status='finish'" +
				" and t.bl_buydate>'"+startTime+"' " +
				" and t.bl_saledate<'"+endTime+"' " +
				" GROUP BY su_id " +
				") a, s_sysuser b where a.su_id=b.REC_ID " +
				"ORDER BY a.pal limit 0,20";
	System.out.println(sql);
	List<HashMap> list = commonDAO.getDataList(sql);
	return list;
	}
	@Override
	public String getDayStatic() {
		StringBuffer sb = new StringBuffer();
		// 计算每日注册数
		String num = "";
		String date = DateFormater.getToday()+" 00:00:00";
		String sql = "SELECT count(0) cnt from s_sysuser where REC_TIME>'"+date+"'";
		List list = commonDAO.getDataList(sql);
		if(null!=list && !list.isEmpty()){
			HashMap map = (HashMap) list.get(0);
			num= StrUtils.getNotNullStrValue(map.get("cnt"));
		}
		sb.append("<tr><td>注册量</td><td>"+num+"</td></tr>");
		
		// 计算代理商推荐用户数
		sql = "SELECT count(0) cnt from s_sysuser where SU_PARDPY is not null and  REC_TIME>'"+date+"'";
		list = commonDAO.getDataList(sql);
		if(null!=list && !list.isEmpty()){
			HashMap map = (HashMap) list.get(0);
			num= StrUtils.getNotNullStrValue(map.get("cnt"));
		}
		sb.append("<tr><td>推荐用户数</td><td>"+num+"</td></tr>");
		
		// 计算买入手数
		sql = "SELECT SUM(bl_num) cnt from t_tradelog where REC_TIME>'"+date+"'";
		list = commonDAO.getDataList(sql);
		if(null!=list && !list.isEmpty()){
			HashMap map = (HashMap) list.get(0);
			num= StrUtils.getNotNullStrValue(map.get("cnt"));
		}
		sb.append("<tr><td>买入手数</td><td>"+num+"</td></tr>");
		// 计算卖出手数
		sql = "SELECT SUM(bl_num) cnt from t_tradelog where bl_status='finish' and REC_TIME>'"+date+"'";
		list = commonDAO.getDataList(sql);
		if(null!=list && !list.isEmpty()){
			HashMap map = (HashMap) list.get(0);
			num= StrUtils.getNotNullStrValue(map.get("cnt"));
		}
		sb.append("<tr><td>卖出手数</td><td>"+num+"</td></tr>");
		return sb.toString();
	}

}
